<?php
/*
 * *	FILE NAME: 	WebUser.php
 * * 	DESCRIPTION: 
 * *	 
 * *	LICENSE:	
 * *
 * *	@programmer	V.Zang Loo
 * *	@license	
 * *	@version	Release: @1.0@
 * *	@link		http://www.domainname.com/package/packagename
 * *	@since		File available since Release 1.0
 * *
 */
class WebUser extends CWebUser {

    public $defaultImage;
    private $_model;
    
    // This is a function that checks the field 'role'
    // in the User model to be equal to 1, that means it's admin
    // access it by Yii::app()->user->isAdmin()
    public function loadModel() {
        if ($this->_model === null) {
            $this->_model = User::model()->with(array(
                'account',
            ))->findByPk((int) Yii::app()->user->id);
        }//end if
        return $this->_model;
    }
    
    public function getRole() {
        return $this->loadModel()->role;
    }
    
    public function getRoleCode(){
        return $this->loadModel()->roleCode;
    }

    public function getHasAccount(){
        return !$this->isGuest && $this->loadModel()->hasAccount;
    }
    
    public function getAccountUrl(){
        return $this->getHasAccount()? $this->loadModel()->account->url:'';
    }
    
    public function getAccountId(){
        return !$this->isGuest && $this->loadModel()->hasAccount? $this->loadModel()->accountId :null;
    }

    public function getIsAdmin() {
        return !$this->isGuest && $this->loadModel()->isAdmin;
    }    
    
    public function getIsMerchant(){
        return !$this->isGuest && $this->loadModel()->isMerchant;
    }
    
    public function getIsMember(){
        return !$this->isGuest && $this->loadModel()->isMember;
    }
    
    public function getIsEditMode(){
        return !$this->isGuest && $this->loadModel()->isEditMode;
    }
    
    public function getIsSocialUser(){
        return !$this->isGuest && $this->loadModel()->isSocialUser;
    }
    
    public function getSquareAvatar(){
        $this->loadModel()->defaultImage=$this->defaultImage;
        return $this->_model->squareAvatar;
    }
    
    public function getLargeSquareAvatar(){
        $this->loadModel()->defaultImage=$this->defaultImage;
        return $this->_model->largeSquareAvatar;
    }
    
    public function getAvatar(){
        $this->loadModel()->defaultImage=$this->defaultImage;
        return $this->_model->avatar;
    }
    
    public function getSmallAvatar(){
        $this->loadModel()->defaultImage=$this->defaultImage;
        return $this->_model->smallAvatar;
    }
    
    public function getMediumAvatar(){
        $this->loadModel()->defaultImage=$this->defaultImage;
        return $this->_model->mediumAvatar;
    }
    
    public function getOriginalAvatar(){
        $this->loadModel()->defaultImage=$this->defaultImage;
        return $this->_model->originalAvatar;
    }
    
    public function getDisplayName() {
        return $this->loadModel()->displayName;
    }

    public function getFullName() {
        return $this->loadModel()->fullName;
    }

    public function getPreferredLanguageCode(){
        return $this->loadModel()->preferredLanguageCode;
    }
    
    public function getEmail() {
        return $this->loadModel()->email;
    }

    public function getContactNumber() {
        return $this->loadModel()->contactNumber;
    }
    
    public function recordUrlReferrer(){
        if(!isset(Yii::app()->session['urlReferrer'])){
            if(!in_array(Yii::app()->request->urlReferrer,array(
                Yii::app()->request->url,
                Yii::app()->createAbsoluteUrl('user/auth/login'),
                Yii::app()->createAbsoluteUrl('user/auth/recovery'),
                Yii::app()->createAbsoluteUrl('user/auth/setnewpassword'),
                Yii::app()->createAbsoluteUrl('user/auth/register'),
                Yii::app()->createAbsoluteUrl('user/auth/activate'),
            ))){
                Yii::app()->session['urlReferrer']=Yii::app()->request->urlReferrer;
            }
        }
    }
    
    public function getReturnUrl($defaultUrl=null){
        if(isset(Yii::app()->session['urlReferrer'])){
            $returnUrl=Yii::app()->session['urlReferrer'];
            unset(Yii::app()->session['urlReferrer']);
        }else{
            $returnUrl=$this->loadModel()->returnUrl;
        }
        return $returnUrl;
    }
    
    public function getUrl(){
        return $this->loadModel()->url;
    }
    
    public function getTotalNotification(){
        return Enquiry::model()->totalNew;
    }
}//end class